﻿using System;
using System.ComponentModel;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
namespace Read_Unread_Solution.Readed
{
    class DisabledItemEventsScope : SPItemEventReceiver, IDisposable
    {
        bool oldValue;

        public DisabledItemEventsScope()
        {
            this.oldValue = base.EventFiringEnabled;
            base.EventFiringEnabled = false;
        }

        #region IDisposable Members

        public void Dispose()
        {
            base.EventFiringEnabled = oldValue;
        }

        #endregion
    }

    [ToolboxItemAttribute(false)]
    public partial class Readed : WebPart
    {
        // Uncomment the following SecurityPermission attribute only when doing Performance Profiling on a farm solution
        // using the Instrumentation method, and then remove the SecurityPermission attribute when the code is ready
        // for production. Because the SecurityPermission attribute bypasses the security check for callers of
        // your constructor, it's not recommended for production purposes.
        // [System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Assert, UnmanagedCode = true)]
        public Readed()
        {
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            InitializeControl();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            using (DisabledItemEventsScope scope = new DisabledItemEventsScope())
            {
                SPContext.Current.Web.AllowUnsafeUpdates = true;
                try
                {
                    //  ptd-spapp/MainOffice/Forms/DispForm.aspx?ID=118
                   
                    string ItemID = this.Page.Request.QueryString["ID"];
                    SPList Current_List = null;
                     SPUserToken systoken = SPContext.Current.Site.SystemAccount.UserToken;
                     using (SPSite site = new SPSite(SPContext.Current.Site.Url, systoken))
                     {
                         using (SPWeb Current_Web = site.OpenWeb())
                         {
                             Current_Web.AllowUnsafeUpdates = true;
                    try
                    {
                        Current_List = Current_Web.GetListFromWebPartPageUrl(Page.Request.Url.ToString());
                    }
                    catch
                    {
                        string[] URLArray = Page.Request.Url.ToString().Split('/');
                        string List_Name = URLArray[3];
                        SPListCollection site_Lists = Current_Web.Lists;
                        foreach (SPList current in site_Lists)
                        {
                            if (current.RootFolder.Url.Contains(List_Name))
                            {
                                Current_List = current;
                            }
                        }
                    }
                        SPListItem Current_Item = Current_List.GetItemById(int.Parse(ItemID));
                    SPFieldUserValueCollection FieldUserValueCollection = (SPFieldUserValueCollection)Current_Item[Constants.Viewd_By_field];
                    SPFieldUserValueCollection fieldUserValues = new SPFieldUserValueCollection();
                    if (FieldUserValueCollection != null)
                    {
                        foreach (SPFieldUserValue FieldUserValue in FieldUserValueCollection)
                        {
                            fieldUserValues.Add(new SPFieldUserValue(SPContext.Current.Web, FieldUserValue.LookupId, FieldUserValue.LookupValue));
                        }
                    }
                    fieldUserValues.Add(new SPFieldUserValue(SPContext.Current.Web, SPContext.Current.Web.CurrentUser.ID, SPContext.Current.Web.CurrentUser.Name));
                    Current_Item[Constants.Viewd_By_field] = fieldUserValues;
                    Current_Item.SystemUpdate(false);
                    Current_Web.AllowUnsafeUpdates = false;
                         }
                     }
                }
                catch
                {
                  
                }
                SPContext.Current.Web.AllowUnsafeUpdates = false;
            }
        }
        

 
               

     

        
    }
}
